package com.atguigu.study.controller;

import com.atguigu.study.entities.User;
import com.atguigu.study.exp.BusinessException;
import com.atguigu.study.resp.ResultData;
import com.atguigu.study.resp.ReturnCodeEnum;
import com.atguigu.study.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther zzyy
 * @create 2024-12-04 17:00
 */
@RestController
@Slf4j
public class UserController
{
    @Resource
    private UserService userService;

    @PostMapping(value = "/user/add")
    public boolean add(@RequestBody User user)
    {
        return userService.save(user);
    }

    @DeleteMapping(value = "/user/del/{id}")
    public boolean del(@PathVariable("id") Integer id)
    {
        return userService.removeById(id);
    }

    @PutMapping(value = "/user/update")
    public boolean update(@RequestBody User user)
    {

        User oldUser = userService.getBaseMapper().selectById(user.getId());

        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper
                .set(User::getUsername,user.getUsername())
                .set(User::getPassword,user.getPassword())
                .set(User::getSex,user.getSex())
                .set(User::getCreateTime,user.getCreateTime())
                .set(User::getVersion,oldUser.getVersion()+1)
                .eq(User::getId,user.getId())
                .eq(User::getVersion,oldUser.getVersion());

        return userService.update(updateWrapper);
    }


    @PutMapping(value = "/user/updateConcurrent")
    public boolean updateConcurrent(@RequestBody User user)
    {

        User oldUser1 = userService.getBaseMapper().selectById(user.getId());
        User oldUser2 = userService.getBaseMapper().selectById(user.getId());

        oldUser1.setPassword("111111");
        userService.getBaseMapper().updateById(oldUser1);

        oldUser2.setPassword("222222");
        int count = userService.getBaseMapper().updateById(oldUser2);

        if(count == 0)
        {
            System.out.println("oldUser2=====update fail");
            return false;
        }else{
            System.out.println("oldUser2=====update success");
            return true;
        }
    }

    @GetMapping(value = "/user/get/{id}")
    public ResultData<User> getById(@PathVariable("id") Integer id)
    {
        if (id < 0) {
            throw new BusinessException(ReturnCodeEnum.REQPARAM_NEGATIVE.getCode(),ReturnCodeEnum.REQPARAM_NEGATIVE.getMessage());
        }
        return ResultData.success(userService.getById(id));
    }

    @GetMapping(value = "/user/list")
    public List<User> list()
    {
        return userService.getBaseMapper().selectList(null);
    }

    @GetMapping(value = "/user/get")
    public User getByUseName(@RequestParam("username") String username)
    {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);

        return userService.getOne(queryWrapper);
    }
}

